package com.xiaomi.dao.impl;

import com.xiaomi.dao.CartDao;
import com.xiaomi.entity.Cart;
import com.xiaomi.entity.Product;
import com.xiaomi.utils.C3P0Utils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author jy
 * @date 2021/5/24
 */
public class CartDaoImpl implements CartDao {

    private final QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());

    @Override
    public Cart hasCart(int pid, int uid) throws Exception {
        //查询出对应商品的信息,在更新money时需要product对象(Cart实体类中的money使用了product对象),
        //否则在更新时会出现NullPointException
        String sql = "select * from cart c,product p where c.pid = p.pid and c.pid=? and c.uid=?";
        Map<String, Object> map = queryRunner.query(sql, new MapHandler(), pid, uid);
        if (map != null) {
            Cart cart = new Cart();
            Product product = new Product();
            BeanUtils.populate(cart, map);
            BeanUtils.populate(product, map);
            cart.setProduct(product);
            System.out.println(cart);
            return cart;
        }
        return null;
    }

    @Override
    public int update(Cart cart) throws Exception {
        Object[] objects = {cart.getNum(), cart.getMoney(), cart.getPid(), cart.getUid()};
        String sql = "update cart set num=?,money=? where pid=? and uid=? ";
        return queryRunner.update(sql, objects);
    }

    @Override
    public int insertCart(Cart cart) throws Exception {
        Object[] objects = {cart.getPid(), cart.getUid(), cart.getNum(), cart.getMoney()};
        String sql = "insert into cart values(default,?,?,?,?)";
        return queryRunner.update(sql, objects);
    }

    @Override
    public List<Cart> showCartsByUid(int uid) throws Exception {
        //查询出cart和product表关联后的数据,放置在map中,
        //用户购物车有多个数据,传回的是List<Map<String, Object>>
        String sql = "select * from cart c,product p where c.pid=p.pid and uid=?";
        List<Map<String, Object>> maps = queryRunner.query(sql, new MapListHandler(), uid);
        List<Cart> carts = new ArrayList<>();
        if (maps != null) {
            for (Map<String, Object> map : maps) {
                Cart cart = new Cart();
                Product product = new Product();
                BeanUtils.populate(cart, map);
                BeanUtils.populate(product, map);
                cart.setProduct(product);
                carts.add(cart);
            }
            return carts;
        }
        return null;
    }

    @Override
    public int update(int cid, int num, BigDecimal money) throws Exception {
        Object[] objects = {num, money, cid};
        String sql = "update cart set num=?,money=? where cid=?";
        return queryRunner.update(sql, objects);
    }

    @Override
    public int delete(int cid) throws Exception {
        String sql = "delete from cart where cid=?";
        return queryRunner.update(sql, cid);
    }

    @Override
    public int clear(int uid) throws Exception {
        String sql = "delete from cart where uid=?";
        return queryRunner.update(sql, uid);
    }

}
